package com.txl.web.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.txl.domain.ContactBean;
import com.txl.domain.ContactGroupBean;
import com.txl.service.ContactGroupService;
import com.txl.service.ContactService;
import com.txl.utils.WebUtils;

public class AddContactServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		request.setAttribute("rg", id);//获取页面带过来的分组id
		ContactGroupService cgs = new ContactGroupService();
		List list = cgs.get2All();//获取去2级节点
		request.setAttribute("list", list);
		request.getRequestDispatcher("/page/addcontact.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ContactBean contact = WebUtils.request2Bean(request, ContactBean.class);
		ContactService service = new ContactService();
		try {
			service.addContact(contact);
		} catch (Exception e) {
			request.setAttribute("message", "添加失败！");
			request.getRequestDispatcher("/message.jsp").forward(request, response);
			return;
		}
		request.setAttribute("message", "添加成功！");
		request.getRequestDispatcher("/message.jsp").forward(request, response);
	}

}

